package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.SupplierMapper;
import com.pojo.TbProducts;
import com.pojo.TbSupplier;
import com.pojo.TbUsers;
import com.service.SupplierService;
import com.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, TbSupplier> implements SupplierService {
    @Autowired
    JSONResult jsonResult;
    @Override
    public JSONResult findAllSupplierBySupplierName(int page, int limit, String supplierName) {
        Page<TbSupplier> tbpage = new Page<>(page,limit);
        if (supplierName!=null&&supplierName!=""){
            QueryWrapper<TbSupplier> tbSupplier = new QueryWrapper<>();
            tbSupplier.eq("supplier_name",supplierName);
            Page<TbSupplier> page1 = this.page(tbpage,tbSupplier);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        Page<TbSupplier> page1 = this.page(tbpage);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }

    @Override
    public JSONResult findAllSupplierByState(int page, int limit, int state) {
        Page<TbSupplier> tbpage = new Page<>(page,limit);
        if (state==1&&state!=-1){
            QueryWrapper<TbSupplier> tbSupplier = new QueryWrapper<>();
            tbSupplier.eq("supplier_state",state);
            Page<TbSupplier> page1 = this.page(tbpage,tbSupplier);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }else if (state==0&&state!=-1){
            Page<TbSupplier> page1 = this.page(tbpage);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        return jsonResult.failure(201,"状态码错误,查询失败",null,null);
    }

    @Override
    public JSONResult addSupplier(TbSupplier supplier) {
        boolean save = this.save(supplier);
        if (save){
            return jsonResult.success(200,"添加成功",null,1L);
        }
        return jsonResult.failure(201,"添加失败",null,0L);
    }

    @Override
    public JSONResult deleteSupplier(int supplierId) {
        boolean b = this.removeById(supplierId);
        if (b){
            return jsonResult.success(200,"删除成功",null,null);
        }
        return jsonResult.failure(201,"删除失败",null,null);
    }

    @Override
    public JSONResult updateSupplier(TbSupplier supplier) {
        boolean b = this.saveOrUpdate(supplier);
        if (b){
            return jsonResult.success(200,"修改成功",null,null);
        }
        return jsonResult.failure(201,"修改失败",null,null);
    }

    @Override
    public JSONResult findSupplierById(int supplierId) {
        QueryWrapper<TbSupplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id",supplierId);
        TbSupplier supplier = this.getOne(queryWrapper);
        if (supplier!=null){
            return jsonResult.success(200,"查询成功",supplier,null);
        }
        return jsonResult.failure(201,"查询失败",null,null);
    }

    @Override
    public JSONResult findAllSupplier(int page, int limit) {
        Page<TbSupplier> tbpage = new Page<>(page,limit);
        QueryWrapper<TbSupplier> tbProducts = new QueryWrapper<>();
        Page<TbSupplier> page1 = this.page(tbpage,tbProducts);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }
}
